package com.dankegongyu.lib.common.widget.dropdown;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;

/**
 * tab item
 *
 * @author WuPuquan
 * @version 1.0
 * @since 2017/9/26 15:57
 */

public class DropDownMenuTabItemView extends LinearLayout {

    // 文字
    private TextView mTvTabItem;
    // 文字右边图片
    private ImageView mIvTabItem;

    // tab文字
    private float mTextSizeDP = 14f;
    private int mTabColorNormal = 0xFF666666;
    private int mTabColorSelected = 0xFF008DF2;

    // 图标normal
    private Drawable mDrawableNormal;
    // 图标selected
    private Drawable mDrawableSelected;

    private boolean mActive = false;

    public DropDownMenuTabItemView(Context context) {
        super(context);

        initTabItemView(context);
    }

    private void initTabItemView(Context context) {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setPadding(DropDownMenuUtil.dp2px(context, 5f), 0, DropDownMenuUtil.dp2px(context, 5f), 0);

        // 文字太长时，右侧图标会被挤出去，为了保证右侧图标可见，再包一层LinearLayout
        LinearLayout llContent = new LinearLayout(getContext());
        llContent.setOrientation(HORIZONTAL);
        llContent.setGravity(Gravity.CENTER);
        addView(llContent);

        mTvTabItem = new TextView(context);
        mTvTabItem.setSingleLine();
        mTvTabItem.setEllipsize(TextUtils.TruncateAt.END);
        // 字体大小
        setTextSizeDP(mTextSizeDP);
        // 设置文字居中
        mTvTabItem.setGravity(Gravity.CENTER);
        // 设置左右padding
        mTvTabItem.setPadding(0, 0, DropDownMenuUtil.dp2px(context, 3f), 0);
        LinearLayout.LayoutParams lpText = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
        llContent.addView(mTvTabItem, lpText);

        mIvTabItem = new ImageView(context);
        mIvTabItem.setPadding(0, 1, 0, 0); // 让箭头偏下点
        LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
        llContent.addView(mIvTabItem, lpImage);
    }

    public DropDownMenuTabItemView setText(String tabName) {
        if (TextUtils.isEmpty(tabName)) {
            mTvTabItem.setVisibility(View.GONE);
        } else {
            mTvTabItem.setVisibility(View.VISIBLE);
            mTvTabItem.setText(tabName);
        }
        return this;
    }

    public DropDownMenuTabItemView setTabGravity(int gravity) {
        setGravity(gravity | Gravity.CENTER_VERTICAL);
        return this;
    }

    /**
     * TextView的宽度如果march_parent，右边的drawable就是在最右边了，
     * 如果是wrap_content, 则跟文字一起居中
     */
    public DropDownMenuTabItemView setTextWidthMarchParent() {
        LinearLayout.LayoutParams lpText = (LayoutParams) mTvTabItem.getLayoutParams();
        lpText.weight = 1;
        mTvTabItem.setLayoutParams(lpText);
        return this;
    }

    public DropDownMenuTabItemView setTextSizeDP(float textSizeDP) {
        this.mTextSizeDP = DropDownMenuUtil.dp2px(getContext(), textSizeDP);
        mTvTabItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSizeDP);
        return this;
    }

    public DropDownMenuTabItemView setTabColorNormal(int textColorNormal) {
        this.mTabColorNormal = textColorNormal;
        return this;
    }

    public DropDownMenuTabItemView setTabColorSelected(int textColorSelected) {
        this.mTabColorSelected = textColorSelected;
        return this;
    }

    public DropDownMenuTabItemView setDrawableNormal(Drawable drawableNormal) {
        this.mDrawableNormal = drawableNormal;
        return this;
    }

    public DropDownMenuTabItemView setDrawableSelected(Drawable drawableSelected) {
        this.mDrawableSelected = drawableSelected;
        return this;
    }

    public DropDownMenuTabItemView setDrawablePadding(int left, int top, int right, int bottom) {
        this.mIvTabItem.setPadding(left, top, right, bottom);
        return this;
    }

    public DropDownMenuTabItemView setTabSelected(boolean selected) {
        if (mActive) {
            // 如果已经是选中状态，则保持
            return this;
        }
        mTvTabItem.setTextColor(selected ? mTabColorSelected : mTabColorNormal);
        mIvTabItem.setImageDrawable(selected ? mDrawableSelected : mDrawableNormal);
        mIvTabItem.setColorFilter(selected ? mTabColorSelected : mTabColorNormal);
//        postInvalidate();
        return this;
    }

    /**
     * 选中状态
     */
    public DropDownMenuTabItemView setActive(boolean active) {
        this.mActive = active;
        mTvTabItem.setTextColor(mActive ? mTabColorSelected : mTabColorNormal);
        mIvTabItem.setImageDrawable(mActive ? mDrawableSelected : mDrawableNormal);
        mIvTabItem.setColorFilter(mActive ? mTabColorSelected : mTabColorNormal);
        return this;
    }

    public String getText() {
        return mTvTabItem.getText().toString().trim();
    }

    public void update(@ColorInt int tabColorNormal, @ColorInt int tabColorSelected) {
        this.mTabColorNormal = tabColorNormal;
        this.mTabColorSelected = tabColorSelected;
        setActive(mActive);
    }
}
